package com.carl.zeus.web.system;

import com.alibaba.fastjson.JSON;
import com.carl.zeus.common.annotation.Log;
import com.carl.zeus.common.constant.BusinessCode;
import com.carl.zeus.common.constant.BusinessMsg;
import com.carl.zeus.common.util.BusinessMsgUtil;
import com.carl.zeus.model.system.SysRole;
import com.carl.zeus.model.system.SysUser;
import com.carl.zeus.model.vo.Tree;
import com.carl.zeus.server.system.SysMenuService;
import com.carl.zeus.server.system.SysRoleService;
import com.carl.zeus.util.MyDES;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 角色管理Controller
 * 
 * @author 作者: zcm
 * @date 创建时间：2017年3月8日 下午1:32:02
 */
@Controller
@RequestMapping(value="sys/role")
public class RoleController {
	
	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysMenuService sysMenuService;
	
	//跳转到role管理页面
	@Log("角色列表")
	@RequestMapping(value="list", method = RequestMethod.GET)
	@RequiresPermissions("sys:role:list")
	public String list() {
		return "system/role";
	}

	/**
	 * 导出角色列表信息
	 * @param role 角色实体
	 * @return
	 */
//	@RequestMapping("/excel_role_export.do")
//	public ModelAndView excelRolesExport(SysRole role){
//		ExcelExport excelExport = roleService.excelExportRoleList(role);
//		return CommonHelper.getExcelModelAndView(excelExport);
//	}


	/**
	 * 跳转到角色新增页面
	 * @return
	 */
	@Log("添加角色")
	@RequestMapping("/add")
	public String toRoleAddPage(Model model) {
		//新增页面标识
		model.addAttribute("role", new SysRole());
		return "system/role-edit";
	}
	
	//获取角色分页对象
	@RequestMapping(value="listAjax", method = RequestMethod.GET)
	@ResponseBody
	public String listAjax(@RequestParam(value = "page", defaultValue = "1") int page,
						      @RequestParam(value = "limit", defaultValue = "10") int limit,
							  @RequestParam(value = "key", defaultValue = "") String key,
							  @RequestParam(value = "value", defaultValue = "") String value) {
		Map<String, Object> map = new HashMap<>();
		List<SysRole> userList = sysRoleService.listAjax(page, limit, key, value);
		map.put("code",0);
		map.put("msg","");
		map.put("count", sysRoleService.countRoleList());
		map.put("data", userList);
		return JSON.toJSONString(map);
	}
	
	//跳轉到編輯頁面edit
	@Log("编辑角色")
	@RequestMapping(value="edit/{id}")
	public String editPage(@PathVariable("id") Long id,Model model) {
		if(id != null){
			SysRole role = sysRoleService.selectById(id);
			model.addAttribute("role", role);
		}
		return "system/role-edit";
	}

	// 增加和修改
	@Log("保存角色")
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public BusinessMsg edit(SysRole role) {
		role.setUpdateTime(new Date());
		if (StringUtils.isNotEmpty(role.getName()) && role.getCreateTime() == null) {
			// 创建角色
			role.setCreateTime(new Date());
			sysRoleService.insertOrUpdate(role);
		}
		return BusinessMsgUtil.returnCodeMessage(BusinessCode.GLOBAL_SUCCESS);
	}

	/**
	 * 角色授权页面
	 * @param model
	 * @param id 角色Id
	 * @return
	 */
	@Log("授权角色")
	@RequestMapping(value = "/roleGrant/{id}", method = RequestMethod.GET)
	public String roleGrantPage(@PathVariable("id") Long id, Model model){
		SysRole role = sysRoleService.selectRoleMenuByRoleId(id);
		model.addAttribute("role", role);
		return "system/role-grant";
	}

	/**
	 * 获取当前用户所属菜单资源Tree菜单展示
	 */
	@RequestMapping(value = "menuTree", method = RequestMethod.POST)
	@ResponseBody
	public List<Tree> ajaxMenuTreeList(){
		return sysMenuService.selectMenuAllTree();
	}

	/**
	 * 保存角色资源信息
	 * @param roleId        角色Id
	 * @param menuIds   资源菜单Ids
	 * @return
	 */
	@RequestMapping(value = "/saveRoleMenu", method = RequestMethod.POST)
	@ResponseBody
	public BusinessMsg ajaxSaveOrUpdateRoleMenu(@RequestParam("roleId") Long roleId,
												@RequestParam(value = "menuIds[]", required = false) Long[] menuIds ){
		try {
			return sysRoleService.saveOrUpdateSysRoleMenu(roleId, menuIds);
		} catch (Exception e) {
			return BusinessMsgUtil.returnCodeMessage(BusinessCode.ROLE_RES_SAVE_ERROR);
		}
	}

	/**
	 * 失效角色
	 * @return
	 */
	@Log("失效角色")
	@RequestMapping("/fail")
	@ResponseBody
	public BusinessMsg ajaxRoleFail(@RequestParam("roleId") Long roleId){
		try {
			return sysRoleService.updateRoleStatus(roleId);
		} catch (Exception e) {
			return BusinessMsgUtil.returnCodeMessage(BusinessCode.ROLE_FAILK_ERROR);
		}
	}

	/**
	 * 批量失效角色
	 * @param roleIds 角色Id
	 * @return
	 */
	@Log("批量失效角色")
	@RequestMapping("/batchFail")
	@ResponseBody
	public BusinessMsg ajaxRoleBatchFail(@RequestParam(value = "roleIds[]") Long[] roleIds){
		try {
			return sysRoleService.updateRoleBatchStatus(roleIds);
		} catch (Exception e) {
			return BusinessMsgUtil.returnCodeMessage(BusinessCode.ROLE_FAILK_ERROR);
		}
	}


}
